package application.server;

import java.util.ArrayList;
import java.util.List;

import javax.jms.JMSException;
import javax.naming.NamingException;

import application.server.jms.MyProducer;

import model.Loan;
import model.dao.DAOFactory;
import model.dao.LoanDAO;

public class ReminderObserver implements Runnable {

	@Override
	public void run() {
		while (true) {
			checkReminder();
			try {
				Thread.sleep(86400000); //runs everey 24 hours
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
	}
	
	private void checkReminder(){
		List<Loan> loans = DAOFactory.getInstance().getLoanDAO().findAll();
		
		for (Loan loan : loans){
			if (loan.getWarning() > 0){
				try {
					MyProducer.getInstance().sendMessage(loan.getWarning() + ". warning for client "
									+ loan.getClient().getId() + ". Exemplar: " + loan.getExemplar().getId());
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (NamingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}
